using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The CountryCodeTableDB class is responsible for interacting with the database to retrieve and store information
    /// about CountryCodeTable objects.
    /// </summary>
    public class CountryCodeTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of CountryCodeTable from the underlying datasource.
        /// </summary>
        /// <param name="countryCode">The unique CountryCode of the CountryCodeTable in the database.</param>
        /// <returns>An CountryCodeTable when the CountryCode was found in the database, or null otherwise.</returns>
        public static CountryCodeTable GetItem(short countryCode)
        {
            CountryCodeTable myCountryCodeTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocCountryCodeTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@countryCode", countryCode);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myCountryCodeTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myCountryCodeTable;
            }
        }

        /// <summary>
        /// Returns a list with CountryCodeTable objects.
        /// </summary>
        /// <returns>A generics List with the CountryCodeTable objects.</returns>
        public static CountryCodeTableList GetList()
        {
            CountryCodeTableList tempList = new CountryCodeTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocCountryCodeTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a CountryCodeTable in the database.
        /// </summary>
        /// <param name="myCountryCodeTable">The CountryCodeTable instance to save.</param>
        /// <returns>The new CountryCode if the CountryCodeTable is new in the database or the existing CountryCode when an item was updated.</returns>
        public static Int16 Save(CountryCodeTable myCountryCodeTable)
        {
            Int16 result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocCountryCodeTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myCountryCodeTable.CountryCode == -1)
                    {
                        myCommand.Parameters.AddWithValue("@countryCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@countryCode", myCountryCodeTable.CountryCode);
                    }
                    if (string.IsNullOrEmpty(myCountryCodeTable.CountryShortCode))
                    {
                        myCommand.Parameters.AddWithValue("@countryShortCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@countryShortCode", myCountryCodeTable.CountryShortCode);
                    }
                    if (string.IsNullOrEmpty(myCountryCodeTable.CountryName))
                    {
                        myCommand.Parameters.AddWithValue("@countryName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@countryName", myCountryCodeTable.CountryName);
                    }
                    if (string.IsNullOrEmpty(myCountryCodeTable.FullCountryName))
                    {
                        myCommand.Parameters.AddWithValue("@fullCountryName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@fullCountryName", myCountryCodeTable.FullCountryName);
                    }
                    if (string.IsNullOrEmpty(myCountryCodeTable.OtherCountryName))
                    {
                        myCommand.Parameters.AddWithValue("@otherCountryName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@otherCountryName", myCountryCodeTable.OtherCountryName);
                    }
                    if (string.IsNullOrEmpty(myCountryCodeTable.FullOtherCountryName))
                    {
                        myCommand.Parameters.AddWithValue("@fullOtherCountryName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@fullOtherCountryName", myCountryCodeTable.FullOtherCountryName);
                    }
                    if (myCountryCodeTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myCountryCodeTable.Status);
                    }
                    if (myCountryCodeTable.CreatedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", myCountryCodeTable.CreatedDateTime);
                    }
                    if (myCountryCodeTable.ModifiedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", myCountryCodeTable.ModifiedDateTime);
                    }

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt16(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a CountryCodeTable from the database.
        /// </summary>
        /// <param name="countryCode">The CountryCode of the CountryCodeTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(short countryCode)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocCountryCodeTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@countryCode", countryCode);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the CountryCodeTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static CountryCodeTable FillDataRecord(IDataRecord myDataRecord)
        {
            CountryCodeTable myCountryCodeTable = new CountryCodeTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryCode")))
            {
                myCountryCodeTable.CountryCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CountryCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryShortCode")))
            {
                myCountryCodeTable.CountryShortCode = myDataRecord.GetString(myDataRecord.GetOrdinal("CountryShortCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryName")))
            {
                myCountryCodeTable.CountryName = myDataRecord.GetString(myDataRecord.GetOrdinal("CountryName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FullCountryName")))
            {
                myCountryCodeTable.FullCountryName = myDataRecord.GetString(myDataRecord.GetOrdinal("FullCountryName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OtherCountryName")))
            {
                myCountryCodeTable.OtherCountryName = myDataRecord.GetString(myDataRecord.GetOrdinal("OtherCountryName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FullOtherCountryName")))
            {
                myCountryCodeTable.FullOtherCountryName = myDataRecord.GetString(myDataRecord.GetOrdinal("FullOtherCountryName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myCountryCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myCountryCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myCountryCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myCountryCodeTable;
        }
    }

}
